package org.example.string;
/**
 * String 的垃圾回收行为
 * -Xms15m -Xmx15m -XX:+PrintStringTableStatistics -XX:+PrintGCDetails
 * @author youshang
 * @date 2022/03/29 11:01
 **/
public class StringGCTest {
    public static void main(String[] args) {
        int num100 = 100;
        int num1000 = 1000;
        int num100000 = 100000;
        for (int i = 0; i < num100000; i++) {
            String.valueOf(i).intern();
        }
    }
    /*
    执行空的main方法时：
    StringTable statistics:
    Number of buckets       :     60013 =    480104 bytes, avg   8.000
    Number of entries       :       825 =     19800 bytes, avg  24.000
    Number of literals      :       825 =     55936 bytes, avg  67.801

    num100 时： 未触发GC
    StringTable statistics:
    Number of buckets       :     60013 =    480104 bytes, avg   8.000
    Number of entries       :       925 =     22200 bytes, avg  24.000
    Number of literals      :       925 =     60736 bytes, avg  65.661

    num1000 时： 未触发GC
    StringTable statistics:
    Number of buckets       :     60013 =    480104 bytes, avg   8.000
    Number of entries       :      1806 =     43344 bytes, avg  24.000
    Number of literals      :      1806 =    103024 bytes, avg  57.045

     num100000 时： 触发GC
     GC details ：
     [GC (Allocation Failure) [PSYoungGen: 4096K->512K(4608K)] 4096K->708K(15872K), 0.0009018 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]

     StringTable statistics:
    Number of buckets       :     60013 =    480104 bytes, avg   8.000
    Number of entries       :     66599 =   1598376 bytes, avg  24.000
    Number of literals      :     66599 =   3739672 bytes, avg  56.152

    总结：当触发GC的时候字符串常量池也是会被回收的
     */
}
